package example.documentbased.editorModule.controller
{
	import example.documentbased.editorModule.models.EditorModuleModel;
	import example.documentbased.mainModule.models.vo.EditorVO;
	import example.documentbased.mainModule.services.delegates.IMainServiceDelegate;
	
	import org.etherframework.controller.IReactiveCommand;
	import org.etherframework.controller.ReactiveCommand;
	
	import raix.reactive.IObservable;
	
	public class SaveEditorCommand extends ReactiveCommand
	{
		private var _editor:EditorVO;
		private var _serviceDelegate:IMainServiceDelegate;
		private var _editorController:IEditorController;
		private var _editorModel:EditorModuleModel;
		
		public function SaveEditorCommand(editorModel:EditorModuleModel, 
										  serviceDelegate:IMainServiceDelegate, 
										  editorController:IEditorController)
		{
			_editorModel = editorModel;
			_serviceDelegate = serviceDelegate;
			_editorController = editorController;
			
			markAsInitialized();
		}
		public function initialize(editor:EditorVO):IReactiveCommand
		{
			_editor = editor;
			
			return this;
		}
		
		override public function observable():IObservable
		{
			return processing(_serviceDelegate.saveEditor(_editor))
			.mapMany(function(id:String):IObservable
			{
				return _editorController.getRefreshAllCommand().observable();
			});
		}
	}
}